package br.com.dev.tcc.view.sinistro;

import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.adapter.ListaApolicesAdapter;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.database.DatabaseHelper;
import br.com.dev.tcc.database.dao.ApoliceDAO;
import br.com.dev.tcc.view.apolice.ApolicesActivity;

import com.actionbarsherlock.app.SherlockFragment;

public class SelecionaApoliceFragment extends SherlockFragment {

	private ProgressBar bar;
	private View containerLista;
	private ListView listApolices;
	private TextView tvSemApolices;
	private OnApoliceSelected listenerLista;

	public SelecionaApoliceFragment(OnApoliceSelected listener){
		listenerLista = listener;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.sinistro_fragment_criar, null);

		containerLista = view.findViewById(R.id.containerLista);
		bar = (ProgressBar) view.findViewById(R.id.progressBar);
		listApolices = (ListView) view.findViewById(R.id.list);
		tvSemApolices = (TextView) view.findViewById(R.id.tvSemItens);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		new RequestApolicesAsync().execute();

		tvSemApolices.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getActivity(), ApolicesActivity.class), 1);
			}
		});
	}

	/*
	 * Async que busca as apólices cadastradas e as exibe na listview
	 */
	private class RequestApolicesAsync extends
			AsyncTask<Void, Void, List<Apolice>> {

		@Override
		protected void onPreExecute() {
			bar.setVisibility(View.VISIBLE);
			containerLista.setVisibility(View.GONE);
			tvSemApolices.setVisibility(View.GONE);
		}

		@Override
		protected List<Apolice> doInBackground(Void... params) {

			ApoliceDAO dao = new ApoliceDAO(new DatabaseHelper(getActivity()));
			return dao.lista();
		}

		@Override
		protected void onPostExecute(final List<Apolice> result) {

			bar.setVisibility(View.GONE);

			if (result != null && result.size() > 0) {

				containerLista.setVisibility(View.VISIBLE);

				listApolices.setAdapter(new ListaApolicesAdapter(getActivity(),
						result));
				listApolices.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> a, View v,
							int position, long id) {
						listenerLista.onSelected(result.get(position));
					}
				});
			} 
			else {

				tvSemApolices.setVisibility(View.VISIBLE);
			}
		}
	}
	
	public void atualiza() {
		new RequestApolicesAsync().execute();
	}
	
	public interface OnApoliceSelected{
		public void onSelected(Apolice apolice);
	}
}
